package services;

import entities.User;
import entities.AdminUser;
import entities.NormalUser;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class UserService {
    private Map<String,User> users = new HashMap<>();
    private Scanner scanner = new Scanner(System.in);

    public UserService(){
        users.put("小虎",new AdminUser("小虎"));
        users.put("小明",new NormalUser("小明"));
        users.put("小花",new NormalUser("小花"));
    }

    public User login() {
        while (true) {
            System.out.println("请输入您的姓名：");
            String name = scanner.nextLine();

            System.out.println("请选择身份（管理员/用户）：");
            String role = scanner.nextLine().trim().toLowerCase();

            if ("退出系统".equals(role)) {
                System.out.println("程序已结束。");
                scanner.close();
                return null;
            }

            boolean isAdminUser = "管理员".equals(role) || "admin".equals(role);
            boolean isNormalUser = "用户".equals(role) || "normal".equals(role);

            if (!isAdminUser && !isNormalUser) {
                System.out.println("无效的身份选择，请重新尝试。");
                continue;
            }

            User user = users.get(name);

            if(user == null){
                System.out.println("用户用无效，请重试。");
                continue;
            }

            if ((isAdminUser && user instanceof AdminUser) || (isNormalUser && user instanceof NormalUser)){
                return user;
            } else {
                System.out.println("身份与用户名不符合，请重试。");
            }
        }
    }
}
